package com.example.team.service.Imp;

import com.example.team.dao.ICpcDao;
import com.example.team.entity.Cpc;
import com.example.team.service.ICpcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
//服务接口规范的方法实现
//编写者：沈超
// 时间：2023/10/29
@Component
public class ICpcServiceImpl implements ICpcService {
    @Autowired
    private ICpcDao iCpcDao;


    @Override
    public int insertCpc(Cpc cpc) {
        //获取当前时间。
        Date now=new Date();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String nowStr=formatter.format(calendar.getTime());
        try {
            //字符串类型转换时间类型
            now = formatter.parse(nowStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date cpcTime=new Date();
        if (cpc.getCpcTime()!=null) {
            //获取原始日期
            cpcTime = cpc.getCpcTime();
            //计算输入日期与当前日期差值
            cpc.setCpcAge((int) ((((now.getTime() - cpcTime.getTime()) / (24 * 60 * 60 * 1000))) / 365));
        }
        return iCpcDao.insertCpc(cpc);
    }

    @Override
    public int deleteCpc(Cpc cpc) {
        return iCpcDao.deleteCpc(cpc);
    }

    @Override
    public List<Cpc> getAllCpc(Cpc cpc) {
        return iCpcDao.getAllCpc(cpc);
    }

    @Override
    public int updataCpc(Cpc cpc) {
        //获取当前时间
        Date now=new Date();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String nowStr=formatter.format(calendar.getTime());
        try {
            //字符串类型转换时间类型
            now = formatter.parse(nowStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date cpcTime=new Date();
        if (cpc.getCpcTime()!=null) {
            //获取原始日期
            cpcTime = cpc.getCpcTime();
            //计算输入日期与当前日期差值
            cpc.setCpcAge((int) ((((now.getTime() - cpcTime.getTime()) / (24 * 60 * 60 * 1000))) / 365));
        }
        return iCpcDao.updataCpc(cpc);
    }

    @Override
    public Cpc getCpc(Cpc cpc) {
        return iCpcDao.getCpc(cpc);
    }
}
